﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data.Objects;
using GlobalObjects;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Data.Entity.Infrastructure;
using WCFServiceWebRole1.DataObjects;
using System.ServiceModel.Web;
using System.Net;

namespace DataAccessLayer
{
    public class SharedEntity
    {
        /// <summary>
        /// Adds a new record in the database in order to associate a device with a new user
        /// to share the data with
        /// </summary>
        /// <param name="device">device id of the device which will share data</param>
        /// <param name="user">user id of the user who will subscribe to the device</param>
        public void shareDeviceWithUser_DAL(int device, int user)
        {
            try
            {
                using (var context = new MSR2DBEntities())
                {
                  var query = from d in context.devices
                              where d.id == device && d.user_id == user
                              select d;
                  if (query.Count() == 0)
                  {
                      var nshared = new shared()
                      {
                          device_id = device,
                          user_id = user
                      };
                      context.shareds.Add(nshared);
                      context.SaveChanges();
                  }
                  else {
                      DbUpdateException exc2 = new DbUpdateException("the user is the owner");
                      throw exc2;
                  }
                }
            }
            catch (DbUpdateException exc)
            {
                DbUpdateException exc2 = new DbUpdateException("error in shareDeviceWithUser_DAL. device_id or user_id does not exist");
                throw exc2;
            }
        }
        /// <summary>
        /// removes a given record from the database based on the given user id and device id
        /// </summary>
        /// <param name="deviceId">device id</param>
        /// <param name="userId">user id</param>
        public void removeSharedDevice(int deviceId, int userId) {
            using (var context = new MSR2DBEntities())
            {
                var query = from s in context.shareds
                            where s.device_id == deviceId && s.user_id == userId
                            select s;
                if (query.Count() == 1)
                {
                    context.shareds.Remove(query.First());
                    context.SaveChanges();
                }
                else {
                    DbUpdateException exc = new DbUpdateException("error in shareDeviceWithUser_DAL. device_id or user_id does not exist");
                    throw exc;
                }
            }
        }
    }
}
